package com.chen.controller.admin;

import com.alibaba.fastjson.JSON;
import com.chen.pojo.Court;
import com.chen.pojo.Type;
import com.chen.service.court.CourtService;
import com.chen.util.DataGridViewResult;
import com.chen.util.SystemConstrant;
import com.chen.vo.CourtVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/court")
public class CourtController {

    @Resource
    private CourtService courtService;

    /**
     * 查询球场列表
     * @param courtVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(CourtVo courtVo){
        //设置分页信息
        PageHelper.startPage(courtVo.getPage(),courtVo.getLimit());
        //调用分页查询方法
        List<Court> courtList = courtService.list(courtVo);
        //创建分页对象
        PageInfo<Court> pageInfo = new PageInfo<Court>(courtList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     *  添加角色列表
     * @param court
     * @return
     */
    @RequestMapping(value = "/addCourt",produces = {"application/json;charset=UTF-8"})
    public String addCourt(Court court){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courtService.addCourt(court)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     *  修改角色列表
     * @param court
     * @return
     */
    @RequestMapping(value = "/updateCourt",produces = {"application/json;charset=UTF-8"})
    public String updateCourt(Court court){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courtService.updateCourt(court)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     *  修改角色列表
     * @param courtId
     * @return
     */
    @RequestMapping(value = "/deleteById",produces = {"application/json;charset=UTF-8"})
    public String deleteById(Integer courtId){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courtService.deleteById(courtId)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
}
